package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.LogConstant;
import com.itheima.dao.LogDao;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.tools.javac.util.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * @version V1.0
 * @author: min
 * @date: 2019/8/7 下午11:16
 * @description:
 */
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogDao logDao;

    /**
     * @param operator:操作者
     * @param time：操作时间
     * @param operateName：操作名称
     * @param operateTypeName：操作类型名称
     * @param args：参数列表
     */
    @Override
    public void add(String operator, String time, String operateName, String operateTypeName, Object[] args, Object rvt) {
        // 操作id
        Integer operatedId = null;
        // 操作类型
        String operateType = null;
        Object arg = null;
        if (args != null && args.length > 0) {
            arg = args[0];
        }
        // 根据fang类型获取方法判断
        if (operateName.equalsIgnoreCase(LogConstant.OPERATE_NAME_ORDERSETTING)) {
            // 订单管理类
            switch (operateTypeName) {
                case "upload":
                    operateType = LogConstant.OPERATE_TYPE_ADD;
                    break;
                case "setNumberByOrderDate":
                    operateType = LogConstant.OPERATE_TYPE_EDIT;
                    break;
                default:
                    break;
            }
            if (!StringUtils.isEmpty(operateType) && operateType.equalsIgnoreCase(LogConstant.OPERATE_TYPE_EDIT)) {
                operatedId = ((OrderSetting) arg).getId();
            }
        } else {
            // 其他类别
            switch (operateTypeName) {
                case "add":
                    operateType = LogConstant.OPERATE_TYPE_ADD;
                    Result result = (Result) rvt;
                    if (!result.isFlag()) {
                        return;
                    }
                    arg = result.getData();
                    System.out.println(arg);
                    break;
                case "edit":
                    operateType = LogConstant.OPERATE_TYPE_EDIT;
                    break;
                case "delete":
                    operateType = LogConstant.OPERATE_TYPE_DELETE;
                    break;
                default:
                    return;
            }

            if (arg instanceof CheckGroup) {
                operatedId = ((CheckGroup) arg).getId();
            } else if (arg instanceof CheckItem) {
                operatedId = ((CheckItem) arg).getId();
            } else if (arg instanceof Setmeal) {
                operatedId = ((Setmeal) arg).getId();
            } else if (arg instanceof Integer) {
                operatedId = (Integer) arg;
            }
        }
        logDao.addLog(operator, time, operateName, operateType, operatedId);
    }
}